package com.bluedot.www.core.dao;

import com.bluedot.www.framework.ibatis.mybaits.executor.SimpleExecutor;

import java.util.List;

/**
 * 数据库操作包装类
 *
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/8/16 16:18
 */
public class Execute {
    public final static Integer QUERY_TYPE = 0;
    public final static Integer UPDATE_TYPE = 1;
    private final static SimpleExecutor simpleExecutor = new SimpleExecutor();

    private Execute() {
    }

    /**
     * 包装查询
     *
     * @param mapperName mapper名字
     * @param objects    参数
     * @return java.util.List<E>
     * @author GUOZHIPENG
     * @date 2021/8/16 16:23
     */
    private static <E> List<E> query(String mapperName, Object[] objects) {
        return simpleExecutor.query(mapperName, objects);
    }

    /**
     * 包装更新
     *
     * @param mapperName mapper名字
     * @param objects    参数
     * @return int
     * @author GUOZHIPENG
     * @date 2021/8/16 16:23
     */
    private static int update(String mapperName, Object[] objects) {
        return simpleExecutor.update(mapperName, objects);
    }

    /**
     * service层调用dao的唯一方法
     *
     * @param optionType 执行方法类型
     * @param mapperName mapper名字
     * @param objects    参数
     * @return java.lang.Object
     * @author GUOZHIPENG
     * @date 2021/8/16 16:33
     */
    public static Object execute(int optionType, String mapperName, Object[] objects) {
        if (QUERY_TYPE == optionType) {
            return query(mapperName, objects);
        } else if (UPDATE_TYPE == optionType) {
            return update(mapperName, objects);
        }
        throw new RuntimeException("数据库操作类型不存在(name) 参数错误");
    }
}
